package com.icss.ah.shopVipSet.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.shopVipSet.bean.ShopVipSetBean;
import com.icss.j2ee.util.UUID;

/**
 * 
 * @author wangjun
 *
 */
@Service
@Transactional
public class ShopVipSetService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;

	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	
	public List getVipList(HashMap map) {
		
		List list = new ArrayList();
		try{
			//根据登录账号获取账号基本信息
			list = sqlSessionTemplate.selectList("ShopVipSetBean.queryVipList", map);
			infolog.info("ShopVipSetService|getVipList|list:"+list);
			list = list == null ? new ArrayList() : list;
		} catch (Exception e) {
			e.printStackTrace();
			list = new ArrayList();
			errlog.error("ShopVipSetService|getVipList", e);
		}
		return list;
		
	}
	
	/**
	 * 分类列表结果集统计
	 * @param map
	 * @return
	 */
	public Long getVipCount(HashMap map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne("ShopVipSetBean.queryVipCount", map);
			infolog.info("ShopVipSetService|getVipCount|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ShopVipSetService|getVipCount", e);
		} 
		return reValue;
	}
	
	/**
	 * 更新状态
	 * @param map
	 */
	public void updateStatus(HashMap map){
		try {
			sqlSessionTemplate.update("ShopVipSetBean.updateStatus", map);
		} catch (RuntimeException e) {
			e.printStackTrace();
			errlog.error("ShopVipSetService|updateStatus", e);
		}
	}
	
	/**
	 * 查询要编辑的店铺会员信息
	 * @param map
	 * @return
	 */
	public HashMap queryTheShopVip(HashMap map) {
		HashMap vipMap=new HashMap();
		try {
			vipMap = (HashMap)sqlSessionTemplate.selectOne("ShopVipSetBean.queryTheShopVip", map);
			infolog.info("ShopVipSetService|queryTheShopVip|vipMap:"+vipMap);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ShopVipSetService|queryTheShopVip", e);
		} 
		return vipMap;
	}
	
	/**
	 * 修改时查询下一级别的信息
	 * @param map
	 * @return
	 */
	public HashMap querytheNextVip(HashMap map) {
		HashMap vipMap=new HashMap();
		try {
			vipMap = (HashMap)sqlSessionTemplate.selectOne("ShopVipSetBean.querytheNextVip", map);
			infolog.info("ShopVipSetService|querytheNextVip|vipMap:"+vipMap);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ShopVipSetService|querytheNextVip", e);
		} 
		return vipMap;
	}
	
	public HashMap queryTheBefShopVip(HashMap map) {
		HashMap vipMap=new HashMap();
		try {
			vipMap = (HashMap)sqlSessionTemplate.selectOne("ShopVipSetBean.queryTheBefShopVip", map);
			vipMap = vipMap == null ? new HashMap() : vipMap;
			if(vipMap.isEmpty()){
				vipMap.put("BEG_MONEY", "0");
				vipMap.put("END_MONEY", "0");
				vipMap.put("BEG_NUM", "0");
				vipMap.put("END_NUM", "0");
			}
			infolog.info("ShopVipSetService|queryTheBefShopVip|vipMap:"+vipMap);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ShopVipSetService|queryTheBefShopVip", e);
		} 
		return vipMap;
	}
	
	/**
	 * 保存会员信息
	 * @param map
	 * @return
	 */
	public boolean saveVipInfo(ShopVipSetBean shopVip,String act,String beforeSorta,String uuid){
		boolean reValue = false;
		try {
			if(act.equals("add")){
				if(uuid.equals("")){
					uuid = new UUID().toString();
				}
				shopVip.setUuid(uuid);
				sqlSessionTemplate.insert("ShopVipSetBean.insertShopVip", shopVip);
			} else {
				shopVip.setUuid(uuid);
				System.out.println("uuid---------------"+uuid);
				sqlSessionTemplate.update("ShopVipSetBean.updateShopVip", shopVip);
			}
			reValue = true;
			infolog.info("ShopVipSetService|saveVipInfo|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ShopVipSetService|saveVipInfo", e);
		} 
		return reValue;
	}
	
	
	
}
